
import wx

import wx.gizmos as gizmos

import IconImages
import ConfigUtil

foreground = None
background = None
font = None

def SetConfigDict(d):
	global foreground, background, font
	if d.has_key('variables_foreground'):
		foreground = d['variables_foreground']
	if d.has_key('variables_background'):
		background = d['variables_background']
	if d.has_key('variables_font'):
		font = d['variables_font']

class VariablesPanel(wx.Panel):
	def __init__(self, parent, position = -100):
		wx.Panel.__init__(self, parent, -1)
		self.Bind(wx.EVT_SIZE, self.OnSize)

		self.splitter = wx.SplitterWindow(self, style=wx.BORDER_NONE)

		self.GenerateRootTree(self.splitter)

		self.text = wx.TextCtrl(self.splitter, -1, style=wx.TE_MULTILINE|wx.TE_RICH)

		self.splitter.SetMinimumPaneSize(20)
		self.splitter.SplitHorizontally(self.tree, self.text, position)

		if font:
			self.tree.SetFont(ConfigUtil.ParseFont(font))
			self.text.SetFont(ConfigUtil.ParseFont(font))
		if foreground:
			self.tree.SetForegroundColour(foreground)
			self.text.SetForegroundColour(foreground)
		if background:
			self.tree.SetBackgroundColour(background)
			self.text.SetBackgroundColour(background)

	def GenerateRootTree(self, parent):
		self.tree = gizmos.TreeListCtrl(parent, -1, style =
				wx.TR_DEFAULT_STYLE
				| wx.TR_HIDE_ROOT
				#| wx.TR_HAS_BUTTONS
				#| wx.TR_TWIST_BUTTONS
				| wx.TR_ROW_LINES
				| wx.TR_COLUMN_LINES
				| wx.TR_NO_LINES 
				| wx.TR_FULL_ROW_HIGHLIGHT
				)

		isz = (16,16)
		il = wx.ImageList(isz[0], isz[1])
		self.iconLocal   = il.Add(IconImages.getVariableLocalBitmap())
		self.iconGlobal  = il.Add(IconImages.getVariableGlobalBitmap())
		self.iconUpvalue = il.Add(IconImages.getVariableUpvalueBitmap())
		self.iconItem    = il.Add(IconImages.getVariableItemBitmap())
		self.iconKey     = il.Add(IconImages.getVariableKeyBitmap())

		self.tree.SetImageList(il)
		self.il = il

		# create some columns
		self.tree.AddColumn("Name")
		self.tree.AddColumn("Value")
		self.tree.SetMainColumn(0) # the one with the tree in it...

		self.root = self.tree.AddRoot("Variables")
		self.tree.SetItemText(self.root, "value", 1)
		self.tree.SetItemImage(self.root, self.iconLocal, which = wx.TreeItemIcon_Normal)
		self.tree.SetItemImage(self.root, self.iconLocal, which = wx.TreeItemIcon_Expanded)

		self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelect)

		return self.tree

	def DisplayVariables(self, variables):
		self.ClearVariables()
		self.AppendVariables(variables)

	def ClearVariables(self):
		self.tree.DeleteChildren(self.root)

	def AppendVariables(self, variables):
		self.AppendVariablesWithIcon(variables, self.iconLocal)
	
	def AppendLocals(self, variables):
		self.AppendVariablesWithIcon(variables, self.iconLocal)

	def AppendGlobals(self, variables):
		self.AppendVariablesWithIcon(variables, self.iconGlobal)

	def AppendUpvalues(self, variables):
		self.AppendVariablesWithIcon(variables, self.iconUpvalue)

	def AppendVariablesWithIcon(self, variables, icon):
		for v in variables:
			self.AppendVariable(self.root, v[0], v[1], icon)
		self.tree.Expand(self.root)
		self.tree.Update()

	def AppendVariable(self, node, k, v, icon):
		#print k, v
		node = self.tree.AppendItem(node, str(k))
		self.tree.SetPyData(node, (k, v, str(v)))

		self.tree.SetItemText(node, self.ShortStr(str(v)), 1)
		self.tree.SetItemImage(node, icon, which = wx.TreeItemIcon_Normal)
		self.tree.SetItemImage(node, icon, which = wx.TreeItemIcon_Expanded)

		if type(v) == list or type(v) == tuple:
			for i in range(len(v)):
				self.AppendVariable(node, i, v[i], self.iconItem)
		elif type(v) == dict:
			for k, v in v.items():
				self.AppendVariable(node, k, v, self.iconKey)
		else:
			pass

	def OnSize(self, event):
		size = self.GetSize()
		self.splitter.SetSize(size)
		w, h = size
		h = h < 150 and -10 or -h/3
		self.splitter.SetSashPosition(h)
		
		w = w - 20
		self.tree.SetColumnWidth(0, w/2)
		self.tree.SetColumnWidth(1, w/2)

	def OnSelect(self, event):
		item = event.GetItem()
		data = self.tree.GetPyData(item)
		if data:
			self.text.SetValue(data[2])

	def ShortStr(self, s):
		if len(s) > 20:
			s = s[0:18] + '...'
		return s


def runTest(frame, nb, log):
	win = VariablesPanel(nb)
	variables = [
			('name', 2),
			('l', ['x', 'y', 'z']),
			('d', {'tom':77, 'jim':[4, 5, 6, {'x':2, 'y':3}]}),
			]
	print variables
	win.DisplayVariables(variables)
	return win



if __name__ == '__main__':
	import sys,os
	import wxrun
	wxrun.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

